$(function(){             
    var configTable = null;  
    var opts = {
        targetId: "configTable",
        templateId: "tableconfig",
        settings: {
            url:DI.queryConfig,
            ajaxType:"json",
            sourceInterface:{INum:"total",IData:"data"},
            paginationIndexName:{
                "peerpageRows":"",
                "currentPage":""
            }
        },
        customFuncs:{
//          delConfig: function(){
//              $("#popup_del").show();
//          },
            editConfig: function(row){
                mainCtrl.isedit = true;
                editCtrl.servicename = row.serviceNumName;
                editCtrl.serviceId = row.serviceNumId;
                var systems = row.systemIds.split(",");
                editCtrl.systemList = [];
                for(var i = 0; i < systems.length; i++){
                    editCtrl.systemList.push({servicesystem:systems[i]});
                }
                $(".u-error").hide();
                $("#popup_edit").show();
            }
        }
    };
    configTable = SwiftUI.swiftTable.getInstance(opts);
    
    var thiswinurl = window.location.href,
        searchKey = "authKey",
        authKeyIdx = thiswinurl.indexOf(searchKey),
        authKeyStr = (authKeyIdx==-1)?"":thiswinurl.substring(authKeyIdx+(searchKey.length+1)),
        authkeyparam = (authKeyStr=="")?"":("?authKey="+authKeyStr);
    
    var mainCtrl = avalon.define({
        $id: "mainController",                  
        isedit: false,
        addConfig: function(){
            mainCtrl.isedit = false;
            editCtrl.serviceId = "";
            editCtrl.servicename = "";
            editCtrl.systemList = [{servicesystem:""}];
            $(".u-error").hide();
            $("#popup_edit").show();
            $("input[name=name]").focus();
        },
//      delData: function(){
//          var delid = "";
//          $.ajax({
//              type: "post",
//              dataType: "json",
//              data:{"id":delid},
//              url: DI.deleteConfig,
//              success: function(data){
//                  if(data.retFlag == 1){
//                      
//                  }else{
//                      $("#popup_tips").show().find(".container p").text("删除失败!");
//                  }
//              },
//              error: function(err){
//                  console.log(err);
//              }
//          });
//      }
    });
    
    var editCtrl = avalon.define({
        $id:"editController",
        serviceId:"",
        servicename: "",
        servicesystem: "",
        editConfig: false,
        systemList: [{servicesystem:""}],
        
        saveConfig: function(){
            $(".u-error").hide();
            var isFail = false;
            //校验输入值
            if(editCtrl.servicename == ""){
                $("input[name=name]").next(".u-error").show();
                isFail = true;
            }
            var noSystem = true;
            for(var i = 0; i < editCtrl.systemList.length; i++){
                var sysName = editCtrl.systemList[i].servicesystem;
                if(sysName != ""){
                    noSystem = false;
                }
            }
            if(noSystem){
                $("#popup_edit").find(".u-error").last().show();
                isFail = true;
            }
            //如果校验失败，就不能继续下面的操作
            if(isFail)return;            
            
            var dataParam = {"serviceNumId":editCtrl.serviceId,"serviceNumName":editCtrl.servicename};
            var systems = "";
            for(var i = 0; i < editCtrl.systemList.length; i++){
                var sysName = editCtrl.systemList[i].servicesystem;
                if(systems == ""){
                    systems = sysName;
                }else{
                    systems += ","+sysName;
                }
            }
            dataParam.systemIds = systems;
            $.ajax({
                type: "post",
                dataType: "json",
                data:dataParam,
                url: DI.updateConfig,
                success: function(data){
                    if(data.retFlag == 1){
                        $("#popup_edit").hide();                        
                        $("#popup_tips").show().find(".container div").removeClass("error").addClass("success");
                        $("#popup_tips").find(".container p").text("保存成功!");
                        configTable.refresh();
                    }else{
                        $("#popup_tips").show().find(".container div").removeClass("success").addClass("error");
                        $("#popup_tips").find(".container p").text("保存失败!失败原因:"+data.errorMessage);
                    }
                },
                error: function(err){
                    console.log(err);
                }
            });
            
        },
        addSystem: function(){
            editCtrl.systemList.push({servicesystem:""});
            setTimeout(function(){
                $("#popup_edit").find(".column").last().find(".u-ipt").focus();
            },0);
        },
        removeDom: function(idx){
            if($(this).hasClass("dis")) return;
            editCtrl.systemList.removeAt(idx);
        }
    });
    avalon.scan();
});